<?php
namespace QnuiRings\Controller;
use Think\Controller;
// 软件名称：互动折扣王-连环折扣-倒计时折扣 
// 代码日期：v3.0 2017-5-10
// 作者：杨道泉 
// 文件名：QnuiZhekouAction.class.php 
// 功能： 使用千牛UI发布限时折扣

class QnuiZhekouController extends QnuiBaseController {
	public function create_shop_active(){
		$this->tagName=I("tagName");
		$this->startTime=date("Y-m-d H:i:s",strtotime(I("startTime")));
		$this->endTime=date("Y-m-d H:i:s",strtotime(I("endTime")));
		$this->notice=I("notice");
		$this->itemArea=I("itemArea");
		$this->discountRate=I("discountRate")*100;
		$this->decreaseAmount=I("decreaseAmount")*100;
		$this->zType=I("zType");
		
		if($this->itemArea=="some"){
			//创建部分商品参与的折扣
			Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityAddRequest');
			$req = new \PromotionmiscCommonItemActivityAddRequest;
			$req->setName($this->tagName);
			$req->setDescription($this->notice);
			$req->setStartTime($this->startTime);
			$req->setEndTime($this->endTime);
			$req->setIsUserTag("false");
			$req->setUserTag("NEW_3294304");
			$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
			
			$this->toolid = 2;
		}
		
		if($this->itemArea=="all"){
			//创建全店参与的折扣
			Vendor('taobaosdk/top/request/PromotionmiscItemActivityAddRequest');
			$req = new \PromotionmiscItemActivityAddRequest;
			$req->setName($this->tagName);
			$req->setParticipateRange("0");
			$req->setStartTime($this->startTime);
			$req->setEndTime($this->endTime);
			$req->setIsDecreaseMoney($this->decreaseAmount?"true":"");
			$req->setDecreaseAmount($this->decreaseAmount);
			$req->setIsDiscount($this->discountRate?"true":"");
			$req->setDiscountRate($this->discountRate);
			$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
			$this->toolid = 1;
		}
		
		
		$this->ajaxReturn($resp);
	}

	public function add_items_to_active(){
		$activity_id = I("activity_id");
		$item_id = I("item_id");
		$promotion_type = I("promotion_type");
		$promotion_value = I("promotion_value");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailAddRequest');
		$req = new \PromotionmiscCommonItemDetailAddRequest;
		$req->setActivityId($activity_id);
		$req->setItemId($item_id);
		$req->setPromotionType($promotion_type);
		$req->setPromotionValue($promotion_value);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		if($resp->is_success){
			//$resp->detail_id
			// 成功后保存到数据库，动作要与千牛端公用
			$this->save_items_to_database();
		}
		
		$this->ajaxReturn($resp);
	}
	public function save_items_to_database(){
		return ;
	}
	public function edit_item_in_active(){
		$activity_id = I("activity_id");
		$detail_id = I("detail_id");
		$item_id = I("item_id");
		$promotion_type = I("promotion_type");
		$promotion_value = I("promotion_value");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailUpdateRequest');
		$req = new \PromotionmiscCommonItemDetailUpdateRequest;
		$req->setDetailId($detail_id);
		$req->setActivityId($activity_id);
		$req->setItemId($item_id);
		$req->setPromotionType($promotion_type);
		$req->setPromotionValue($promotion_value);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		if($resp->is_success){
			// 成功后保存到数据库，动作要与千牛端公用
			$this->update_items_to_database();
		}
		$this->ajaxReturn($resp);
	}
	public function update_items_to_database(){
		return ;
	}
	public function remove_items_in_active(){
		$activity_id = I("activity_id");
		$detail_id = I("detail_id");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailDeleteRequest');
		$req = new \PromotionmiscCommonItemDetailDeleteRequest;
		$req->setActivityId($activity_id);
		$req->setDetailId($detail_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function get_items_in_active(){
		$activity_id = I("activity_id");
		$page = I("page");
		$page_size = I("page_size");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemDetailListGetRequest');
		$req = new \PromotionmiscCommonItemDetailListGetRequest;
		$req->setActivityId($activity_id);
		$req->setPageNo($page);
		$req->setPageSize($page_size);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function items_by_ids(){
		$ids = I("ids");
		$fields = I("fields");
		Vendor('taobaosdk/top/request/ItemsSellerListGetRequest');
		$req = new \ItemsSellerListGetRequest;
		$req->setFields(($fields?$fields:"num_iid,title,price,pic_url").",approve_status");
		$req->setNumIids($ids);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function items_on_sale(){
		$q = I("q");
		$cid = I("cid");
		$cids = I("cids");
		$page = I("page");
		$pagesize = I("pagesize");
		$fields = I("fields");
		Vendor('taobaosdk/top/request/ItemsOnsaleGetRequest');
		$req = new \ItemsOnsaleGetRequest;
		$req->setFields($fields?$fields:"num_iid,title,price,pic_url");
		if($q){$req->setQ($q);}
		if($cid){$req->setCid($cid?$cid:"");}
		if($cids){$req->setSellerCids($cids?$cids:"");}
		$req->setPageNo($page?$page:1);
		$req->setPageSize($pagesize?$pagesize:10);
		// $req->setHasDiscount("true");
		// $req->setHasShowcase("true");
		// $req->setOrderBy("list_time:desc");
		// $req->setIsTaobao("true");
		// $req->setIsEx("true");
		// $req->setStartModified("2000-01-01 00:00:00");
		// $req->setEndModified("2000-01-01 00:00:00");
		// $req->setIsCspu("true");
		// $req->setIsCombine("true");
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		

		$this->ajaxReturn($resp);
	}
	public function items_in_depot(){
		
		$q = I("q");
		$cid = I("cid");
		$cids = I("cids");
		$page = I("page");
		$pagesize = I("pagesize");
		$fields = I("fields");
		Vendor('taobaosdk/top/request/ItemsInventoryGetRequest');
		$req = new \ItemsInventoryGetRequest;
		$req->setFields($fields?$fields:"num_iid,title,price,pic_url");
		if($q){$req->setQ($q);}
		if($cid){$req->setCid($cid?$cid:"");}
		if($cids){$req->setSellerCids($cids?$cids:"");}
		$req->setBanner("for_shelved");
		$req->setPageNo($page?$page:1);
		$req->setPageSize($pagesize?$pagesize:10);
		// $req->setHasDiscount("true");
		// $req->setOrderBy("list_time:desc");
		// $req->setIsTaobao("true");
		// $req->setIsEx("true");
		// $req->setStartModified("2000-01-01 00:00:00");
		// $req->setEndModified("2000-01-01 00:00:00");
		// $req->setIsCspu("true");
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
		$this->ajaxReturn($resp);
	}
	public function sellercats_list(){
		$nick = session("VISITOR_NICK")?session("VISITOR_NICK"):I("nick");
		Vendor('taobaosdk/top/request/SellercatsListGetRequest');
		$req = new \SellercatsListGetRequest;
		$req->setNick($nick);
		$req->setFields("cid,name");
		$resp = pre_resp($this->c->execute($req));
		$this->ajaxReturn($resp);
	}
	public function edit_active(){
		$activity_id = I("activity_id");
		$name = I("name");
		$description = I("description");
		$start_time = date("Y-m-d H:i:s",strtotime(I("start_time")));;
		$end_time = date("Y-m-d H:i:s",strtotime(I("end_time")));;
		$is_user_tag = I("is_user_tag");
		$tag_id = I("tag_id");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityUpdateRequest');
		$req = new \PromotionmiscCommonItemActivityUpdateRequest;
		$req->setActivityId($activity_id);
		$req->setName($name);
		$req->setDescription($description);
		$req->setStartTime($start_time);
		$req->setEndTime($end_time);
		$req->setIsUserTag($is_user_tag?true:false);
		$req->setUserTag("NEW_".$tag_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		// $resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function remove_active(){
		$activity_id = I("activity_id");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityDeleteRequest');
		$req = new \PromotionmiscCommonItemActivityDeleteRequest;
		$req->setActivityId($activity_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	
	public function get_active_list(){
		$page = I("page");
		$page_size = I("page_size");
		Vendor('taobaosdk/top/request/PromotionmiscCommonItemActivityListGetRequest');
		$req = new \PromotionmiscCommonItemActivityListGetRequest;
		$req->setPageNo($page);
		$req->setPageSize($page_size);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		
	   $this->ajaxReturn($resp);
	}
	public function get_shop_active_list(){
		$page = I("page");
		$page_size = I("page_size");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityListGetRequest');
		$req = new \PromotionmiscItemActivityListGetRequest;
		$req->setPageNo($page);
		$req->setPageSize($page_size);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function edit_shop_active(){
		$activity_id = I("activity_id");
		$name = I("name");
		$description = I("description");
		$start_time = date("Y-m-d H:i:s",strtotime(I("start_time")));;
		$end_time = date("Y-m-d H:i:s",strtotime(I("end_time")));;
		$is_user_tag = I("is_user_tag");
		$tag_id = I("tag_id");
		$is_decrease_money = I("is_decrease_money");
		$is_discount = I("is_discount");
		$decrease_amount = I("decrease_amount");
		$discount_rate = I("discount_rate");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityUpdateRequest');
		$req = new \PromotionmiscItemActivityUpdateRequest;
		$req->setActivityId($activity_id);
		$req->setName($name);
		$req->setParticipateRange("0");
		$req->setStartTime($start_time);
		$req->setEndTime($end_time);
		$req->setIsUserTag($is_user_tag?"true":"false");
		$req->setUserTag("top".$tag_id);
		$req->setIsDecreaseMoney($is_decrease_money);
		$req->setDecreaseAmount($decrease_amount);
		$req->setIsDiscount($is_discount);
		$req->setDiscountRate($discount_rate);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function get_shop_active_detail(){
		$activity_id = I("activity_id");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityGetRequest');
		$req = new \PromotionmiscItemActivityGetRequest;
		$req->setActivityId($activity_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function remove_shop_active(){
		$activity_id = I("activity_id");
		Vendor('taobaosdk/top/request/PromotionmiscItemActivityDeleteRequest');
		$req = new \PromotionmiscItemActivityDeleteRequest;
		$req->setActivityId($activity_id);
		$resp = pre_resp($this->c->execute($req, session("TOP_SESSION")));
		$this->ajaxReturn($resp);
	}
	public function api(){
		Vendor('taobaosdk/top/request/UmpMbbsGetRequest');
		$req = new \UmpMbbsGetRequest;
		// $req->setType("resource");
		$resp = $this->c->execute($req);
		var_dump($resp);
	}
}